package com.ib.algokit.marketscan;

import java.util.List;

import org.activequant.core.domainmodel.InstrumentSpecification;
import org.activequant.core.domainmodel.marketscanner.MarketScan;
import org.activequant.core.domainmodel.marketscanner.MarketScanRow;
import org.activequant.util.pattern.events.IEventListener;

import com.ib.algokit.TraderProxy;
/**
 * ThreadPerMarketScanEventHandlingStrategy implements IEventListener&lt;MarketScanChange&gt;.<br/>
 * This is the listener for MarketScanChanges that starts a separate Thread to analyze an Instrument that seems interesting.<br/>
 * Holds the associated ema(EMA) variable. It also defines a nested InstrumentAnalyzingThread class.
 * @author dmisev
 *
 */
public class MarketScanListenerHandlingStrategy extends ThreadPerInstrumentBaseStrategy implements IEventListener<MarketScan> {

	/**
	 * private final Map&lt;InstrumentSpecification, InstrumentAnalyzingThread&gt; analyzerThreads = new ConcurrentHashMap&lt;InstrumentSpecification, InstrumentAnalyzingThread&gt;();
	 */
//	private final Map<InstrumentSpecification, InstrumentAnalyzingThread> analyzerThreads = new ConcurrentHashMap<InstrumentSpecification, InstrumentAnalyzingThread>();
	/**
	 * Constructs a ThreadPerMarketScanEventHandlingStrategy(implements IEventListener&lt;MarketScanChange&gt;) using the given ema(EMA) to set its
	 * associated ema(EMA).<br/>
	 * It also iterates the existing Positions and starts an InstrumentAnalyzingThread for those whose quantity is not 0
	 * @param ema
	 */
	public MarketScanListenerHandlingStrategy(TraderProxy ema){
		super(ema);
//		InstrumentSpecification spec=new InstrumentSpecification(new Symbol("AAPL"), "SMART", "IB", Currency.USD, SecurityType.OPTION);
//		analyzeInstrument(spec);
		
//		analyzePositions();		
	}
	/**
	 * This gets notified of any MarketScanChanges and then we choose which ones we will analyze by starting an InstrumentAnalyzingThread fo them
	 */
	public void eventFired(MarketScan marketScan) {	
		List<MarketScanRow> marketScanRows= marketScan.getRows();
//		StringBuilder info=new StringBuilder("code="+marketScan.getScanCriteria().getScanCode()+"@"+marketScan.getTimeStamp().getDate()+" "+marketScan.getRows().size()+"rows");
//		for(MarketScanRow marketScanRow : marketScanRows){
//			InstrumentSpecification instrumentSpecification = marketScanRow.getInstrumentSpecificationDetails().getInstrumentSpecification();
//			info.append(" "+instrumentSpecification.getSymbol());
//		}
//		log.info(info);
		if(ema.getAnalyzerThreads().size()>=MAX_ALLOWED_INSTRUMENTS){
			return;
		}
		for(MarketScanRow marketScanRow : marketScanRows){
			if(ema.getAnalyzerThreads().size()>=MAX_ALLOWED_INSTRUMENTS){
				break;
			}	
			InstrumentSpecification instrumentSpecification = marketScanRow.getInstrumentSpecificationDetails().getInstrumentSpecification();
			InstrumentAnalyzingThread instrumentAnalyzingThread = ema.getAnalyzerThreads().get(instrumentSpecification);
			if (instrumentAnalyzingThread == null) {
				//instrumentSpecification=ema.getInstrumentSpecificationDao().update(instrumentSpecification);			
				instrumentAnalyzingThread = new InstrumentAnalyzingThread(marketScanRow.getInstrumentSpecificationDetails(),false);
				ema.getAnalyzerThreads().put(instrumentSpecification, instrumentAnalyzingThread);
				startAnalyzerThread(instrumentAnalyzingThread);			
			}
			
		}
		
//		InstrumentSpecification instrumentSpecification = marketScan.getRows().get(0).getInstrumentSpecificationDetails().getInstrumentSpecification();
		
//		instrumentAnalyzingThread.enqueue(marketScanChange);
	}
}